<?php

namespace app\index\service;

use app\common\behavior\Init;
use app\common\ConfigBase;
use app\common\model\ToolModel;
use app\index\model\UserModel;
use think\Db;
class UserService
{
    /**
     *  oa账号登陆
     * @param $name
     * @param $pwd
     * @return array
     */
    public static function ssoLogin($name, $pwd)
    {
        $where=[];
        $where['name'] = $name;
        $where['pwd'] = MD5($pwd);
        $data = Db::connect('db_sso')
            ->table('_of_sso_user')
            ->where($where)
            ->find();
        if (empty($data['id'])) {
            return returnArr('2','OA账号或者密码错误');
        } else {
            if($data['state']==0){
                return returnArr('2','OA账号状态异常请联系管理员');
            }
            $user=[];
            $user['id'] = $data['id'];
            $user['name'] = $data['name'];
            $user['cn_name'] = $data['nike'];
            $user['ctime']=TIME;
            $user['utime']=TIME;
            ToolModel::dbInsertOrUpdate('user',[$user]);
            unset($user['ctime'],$user['utime']);
            return self::config($user['id']);
        }
    }
    //获取配置 权限
    public static function config($uid){
        $info=ToolModel::findByAttributes('user',['id'=>$uid]);
        if($info['status']!=1){
            return returnArr('2','用户账号异常请联系管理员');
        }
        session('user',$info);
        $pn=[];
        foreach (Init::$permission as $k=>$v){
            $pn[substr($v,6)]=$k;
        }
        $warehouseCountry=UserModel::getWarehouseCountry();
        return returnArr(1,'登陆成功',['cn_name'=>$info['cn_name']
            ,'access_token' =>$uid
            ,'permission'=>$pn//全部权限
            ,'act_permission'=>$info['act_permission']//可用操作权限
            ,'shop_permission'=>$info['shop_permission']//店铺数据权限
            ,'warehouse_country_permission'=>$info['warehouse_country_permission']//国家仓数据权限
            ,'warehouse_shop_permission'=>$info['warehouse_shop_permission']//店仓数据权限
            ,'company'=>ConfigBase::getCompany()//公司
            ,'companyAll'=>ConfigBase::getCompanyAll()//公司
            ,'country'=>ConfigBase::getCountry()//国家
            ,'shop'=>ConfigBase::getShop()//店铺
            ,'shopAll'=>ConfigBase::getShopAll()//店铺
            ,'warehouse'=>ConfigBase::getWarehouse()//仓库
            ,'warehouseAll'=>ConfigBase::getWarehouseAll()//仓库
            ,'warehouseCountry'=>$warehouseCountry//仓库对应国家
        ]);
    }
    //编辑用户
    public static function edit($param){
        $permission=[];
        $shopPermission=[];
        $countryPermission=[];
        if(!empty($param['auth'])){
            foreach ($param['auth'] as $v){
                //店铺数据权限
                if($v>=20000&&$v<=29999){
                    $shopPermission[]=$v;
                }
                //国家数据权限
                if($v>=30000&&$v<=39999){
                    $countryPermission[]=$v;
                }
                //操作权限
                if($v<10000){
                    $permission[]=$v;
                }
            }
        }
        $whShopId=ToolModel::select('base_warehouse',['base_shop_id'=>['in',$shopPermission]],'id','','id');
        ToolModel::updateData(['id' => $param['uid']], ['role' => $param['role'], 'phone' => $param['phone']
            ,'act_permission' => empty($permission) ? '' : join(',',$permission)
            ,'shop_permission' => empty($shopPermission) ? '' : join(',',$shopPermission)
            ,'warehouse_country_permission' => empty($countryPermission) ? '' : join(',',$countryPermission)
            ,'warehouse_shop_permission' => empty($whShopId) ? '' : join(',',$whShopId)
        ],
            'user');
    }
    //获取编辑用户
    public static function getEditUser($param){
        $user = ToolModel::findByAttributes('user', ['id' => $param['id']]);
        $auth='';
        if(!empty($user['act_permission'])){
            $auth=$user['act_permission'];
            if(!empty($user['shop_permission'])){
                $auth.=','.$user['shop_permission'];
            }
            if(!empty($user['warehouse_country_permission'])){
                $auth.=','.$user['warehouse_country_permission'];
            }
        }
        $user['auth']=$auth;
        return $user;
    }
    //获取用户列表
    public static function getList($param){
        if(empty($param['down'])){
            $titles=[];
        }else{
            $titles=[
                'id'=>'id',
                'name'=>'用户名',
                'cn_name'=>'中文名',
            ];
        }
        return UserModel::getUserList($param,$titles,"\app\index\service\UserService::getListHandle");
    }
    //用户数据处理
    public static function getListHandle($data){
        foreach ($data as &$v){
            $v['statusStr']=$v['status']==1?'启用':'禁用';
        }
        return $data;
    }
    //获取全部权限设置
    public static function getPermission(){
        $permission = include APP_PATH . 'permission.php';
        $shop=ConfigBase::getShop();
        $wh=[];
        $wh[0]=["id" => 20000, "pid" => 0, 'title' => '店铺管理数据权限', 'childs' => []];
        $tempShop=[];
        foreach ($shop as $k=>$v){
            $tempShop[]= ["id" =>$k, "pid" => 20000, 'title' => $v];
        }
        $wh[0]['childs']=$tempShop;

        $ware=ConfigBase::getWarehouseCountry();
        $wh[1]=["id" => 30000, "pid" => 0, 'title' => '国家仓管理数据权限', 'childs' => []];
        $tempCountry=[];
        foreach ($ware as $k=>$v){
            $tempCountry[]= ["id" => $k, "pid" => 30000, 'title' =>$v];
        }
        $wh[1]['childs']=$tempCountry;

        //数据权限 10000公司  20000店铺  30000仓库
        return array_merge($permission,$wh);
    }
}
